#include "Audio.h"

Audio::Audio()
{
	// initialize sdl mixer, open up the audio device
	if(Mix_OpenAudio(44100,MIX_DEFAULT_FORMAT,2,1024)<0)
		printf("Error opening audio! %s\n", Mix_GetError());

	Mix_AllocateChannels(16);
	Mix_Volume(-1, 64);
	Mix_VolumeMusic(30);

	music=Mix_LoadMUS("music/level1.ogg");
	if(!music) {
		printf("Mix_LoadMUS: %s\n", Mix_GetError());
		// this might be a critical error...
	}

}

void Audio::load( const std::string& file )
{
	Mix_Chunk *wave_chunk;
	if(!(wave_chunk=Mix_LoadWAV(file.c_str()))) {
		printf("Mix_QuickLoad_WAV: %s\n", Mix_GetError());
	}

	m_sounds[file] = wave_chunk;
}

void Audio::play( const std::string& file )
{
	soundslist::iterator it = m_sounds.find(file);
	if( it == m_sounds.end() ) 
		load( file );

	if(Mix_PlayChannel(-1, m_sounds[file], 0)==-1) {
		printf("Mix_PlayChannel: %s\n",Mix_GetError());
	}
}

Audio::~Audio()
{
	Mix_CloseAudio();
}

void Audio::playMusic()
{
	if(Mix_PlayMusic(music, -1)==-1) {
		printf("Mix_PlayMusic: %s\n", Mix_GetError());
		// well, there's no music, but most games don't break without music...
	}
}